package com.accounting.client.frontend.order.handlers;

import com.accounting.client.frontend.common.CurrencyBox;
import com.accounting.client.remote.Services;
import com.accounting.client.remote.enumeration.Courier;
import com.accounting.client.remote.obj.Order;
import com.accounting.client.remote.transfer.UpdateOrderRequest;
import com.accounting.client.session.ErrorHandler;
import com.accounting.client.session.SessionManager;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.NumberFormats;
import corelib.client.delegate.Action;
import corelib.client.resource.CommonResources;
import corelib.client.ui.SimpleMask;
import corelib.client.ui.widget.Size;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.WindowHandle;
import corelib.client.ui.widget.button.IsButton;
import corelib.client.ui.widget.dialog.SimpleDialogHandler;
import corelib.client.ui.widget.form.SelectField;
import corelib.client.ui.widget.form.SimpleLabel;
import corelib.client.ui.widget.form.TextField;
import corelib.client.ui.widget.form.fomatters.ValueFormatter;
import corelib.client.ui.widget.layout.RelativeColumn;
import corelib.client.ui.widget.layout.RelativeLayout;

public class UpdateOrderHandler extends SimpleDialogHandler {
	
	private Order transactionInfo;
	private TextField trackingIdField;
	private SelectField<Courier> courierField;
	private CurrencyBox  postageField;
	private CurrencyBox  priceField;
	private Action action;
	
	public UpdateOrderHandler(Order info, Action action) {
		this.action = action;
		this.transactionInfo = info;
	}
	
	@Override
	public String getHeading() {
		return "升级更新";
	}
	
	@Override
	public Widget getContentPanel() {
		courierField = new SelectField<Courier>(Courier.All);
		courierField.setNullable(true);
		courierField.setOptionFormatter("请选择快递", new ValueFormatter<Courier>(){
			@Override
			public String format(Courier value) {
				return value.getCName();
			}
		});
		postageField = new CurrencyBox();
		priceField   = new CurrencyBox();
		priceField.setFormat(NumberFormats.RMB);
		trackingIdField = new TextField();
		
		FlowPanel panel = new FlowPanel();
		panel.getElement().getStyle().setPadding(20, Unit.PX);
		
		int lw  = 100;
		int fw  = 250;
		
		RelativeColumn column = new RelativeColumn();
		column.setRowSpacing(12);
		column.addRow().add(new SimpleLabel("快递").setBold(true),     courierField,    RelativeLayout.createLeftLabel(fw, lw));
		column.addRow().add(new SimpleLabel("快递查询号").setBold(true), trackingIdField, RelativeLayout.createLeftLabel(fw, lw));
		column.addRow().add(new SimpleLabel("订单邮费").setBold(true),  postageField,     RelativeLayout.createLeftLabel(fw, lw));
		column.addRow().add(new SimpleLabel("产品总价").setBold(true),  priceField,       RelativeLayout.createLeftLabel(fw, lw));
		
		panel.add(column.getPanel());
		return panel;
	}

	@Override
	public Size getSize() {
		return new Size(400, 300);
	}
	
	@Override
	public void onShowing() {
		super.onShowing();
		courierField.setSelectedOption(Courier.getInstance(transactionInfo.getCourierId()), false);
		postageField.setDouble(transactionInfo.getPostage(),false);
		trackingIdField.setText (transactionInfo.getTrackingId(), false);
		priceField.setDouble(transactionInfo.getPrice(), false);
	}

	@Override
	public IsButton[] getButtons() {
		return new IsButton[] {
			   new IsButton("OK",CommonResources.INSTANCE.accept16(), 100, new WidgetListener(){
				   @Override
				   public void valueUpdated(Widget widget) {
					   update();
				   }
			   })
		};
	}

	@Override
	public void onClosing(WindowHandle windowHandle) {
		windowHandle.close();
	}
	
	private void update() {
		double postage = postageField.getDouble(0.0);
		double price   = priceField.getDouble(0.0);
		if (courierField.getSelectedOption() == null){
			courierField.markError("请选择快递");
			return;
		}
		
		if (price == 0.0){
			priceField.markError("价格不能为零");
			return;
		}
		
		String passport = SessionManager.getPassport();
		UpdateOrderRequest request = new UpdateOrderRequest();
		request.setOrderId(transactionInfo.getId());
		request.setCourierId(courierField.getSelectedOption().getCode());
		request.setTrackingId(trackingIdField.getTrimmedText());
		request.setPrice(price);
		request.setPostage(postage);
		
		SimpleMask.mask(getDialog(),"努力更新中...");
		Services.OrderService.UpdateOrder(passport, request,
			new AsyncCallback<Boolean>(){
				@Override
				public void onFailure(Throwable caught) {
					SimpleMask.unmask(getDialog());
					ErrorHandler.handleServiceError(caught);
				}
				
				@Override
				public void onSuccess(Boolean result) {
					SimpleMask.unmask(getDialog());
					if (result == Boolean.TRUE){
						if (action != null){
							action.run();
						}
						closeDialog();
					}
				}
			}
		);
	}
}
